package com.sunwayhorizo.health.business.patad.integration.query;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.Query;
import javax.persistence.TemporalType;

import org.jboss.seam.annotations.Name;

import com.sunwayhorizo.health.business.base.query.BaseQuery;
import com.sunwayhorizo.health.common.DateUtil;
import com.sunwayhorizo.health.common.StrUtil;
import com.sunwayhorizo.health.hl7.common.Hl7Exception;
import com.sunwayhorizo.health.hl7.rim.PatientEncounter;
import com.sunwayhorizo.health.hl7.vocabulary.ActCode;
import com.sunwayhorizo.health.hl7.vocabulary.ActStatus;
import com.sunwayhorizo.health.hl7.vocabulary.ParticipationType;
import com.sunwayhorizo.health.hl7.vocabulary.RoleCode;

@Name("PatientQualityControlQuery")
@Stateless
public class PatientQualityControlQuery extends BaseQuery implements PatientQualityControlQueryLocal
{
    @SuppressWarnings("unchecked")
    public List<PatientEncounter> findInPatientByWorkGroupId(String workGroupId, int pageNo, int pageSize) throws Hl7Exception
    {
        conditions.clear();

        Map<String, Object> enc = new HashMap<String, Object>();
        enc.put("codeEtsId", ActCode.INDEPARTMENT.code().stringValue());
        enc.put("statusCode", ActStatus.ACTIVE.code().stringValue());
        conditions.put("act", enc);

        Map<String, Object> part = new HashMap<String, Object>();
        part.put("typeCode", ParticipationType.ELOC.code().toString());
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        r.put("codeEtsId", RoleCode.BED.code().toString());
        conditions.put("r", r);

        Map<String, Object> playEnty = new HashMap<String, Object>();
        conditions.put("playEnty", playEnty);
        
        Map<String, Object> en = new HashMap<String, Object>();
        conditions.put("en", en);
        
        Map<String, Object> scopeEnty = new HashMap<String, Object>();
        conditions.put("scopeEnty", scopeEnty);

        Map<String, Object> playRole = new HashMap<String, Object>();
        playRole.put("roleId", workGroupId);
        conditions.put("playRole", playRole);

        Map<String, Object> playEnty2 = new HashMap<String, Object>();
        conditions.put("playEnty2", playEnty2);
        
        Map<String, Object> en2 = new HashMap<String, Object>();
        conditions.put("en2", en2);
        
        Map<String, Object> enu2 = new HashMap<String, Object>();
        enu2.put("use","L");
        conditions.put("enu2", enu2);
        
        //按病区中文名称、床号排序
        Map<String, Object> orderBy = new HashMap<String, Object>();
        orderBy.put("asc", "en2.formatted,en.formatted");
        conditions.put("order by", orderBy);
        
        List<PatientEncounter> encs = (List<PatientEncounter>) queryResults(conditions, PatientEncounter.class, pageNo, pageSize);
        return encs;
    }

    public Long findCountInPatientByWorkGroupId(String workGroupId) throws Hl7Exception
    {
        conditions.clear();

        Map<String, Object> enc = new HashMap<String, Object>();
        enc.put("codeEtsId", ActCode.INDEPARTMENT.code().stringValue());
        enc.put("statusCode", ActStatus.ACTIVE.code().stringValue());
        conditions.put("act", enc);

        Map<String, Object> part = new HashMap<String, Object>();
        part.put("typeCode", ParticipationType.ELOC.code().toString());
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        r.put("codeEtsId", RoleCode.BED.code().toString());
        conditions.put("r", r);

        Map<String, Object> scopeEnty = new HashMap<String, Object>();
        conditions.put("scopeEnty", scopeEnty);

        Map<String, Object> playRole = new HashMap<String, Object>();
        playRole.put("roleId", workGroupId);
        conditions.put("playRole", playRole);

        Long count = (Long) queryCountResults(conditions, PatientEncounter.class);
        return count;
    }
    
    @SuppressWarnings("unchecked")
    public List<PatientEncounter> findInPatientByWorkGroupIdByCond(String wgId, String criticalCode, String surgeryCode, String transfuseCode,int pageNo, 
        int pageSize) throws Hl7Exception
    {
        conditions.clear();

        Map<String, Object> enc = new HashMap<String, Object>();
        enc.put("codeEtsId", ActCode.INDEPARTMENT.code().stringValue());
        enc.put("statusCode", ActStatus.ACTIVE.code().stringValue());
        conditions.put("act", enc);

        Map<String, Object> cdSet = new HashMap<String, Object>();
        if (StrUtil.isNotAllEmpty(criticalCode, surgeryCode, transfuseCode))
        {
            if (StrUtil.isNotEmpty(criticalCode))
            {
                cdSet.put("attributeTypeCode", criticalCode);
            }
            else if (StrUtil.isNotEmpty(surgeryCode))
            {
                cdSet.put("attributeTypeCode", surgeryCode);
            }
            else if (StrUtil.isNotEmpty(transfuseCode))
            {
                cdSet.put("attributeTypeCode", transfuseCode);
            }
            conditions.put("acd", cdSet);
        }
        
        Map<String, Object> part = new HashMap<String, Object>();
        part.put("typeCode", ParticipationType.ELOC.code().toString());
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        r.put("codeEtsId", RoleCode.BED.code().toString());
        conditions.put("r", r);

        Map<String, Object> playEnty = new HashMap<String, Object>();
        conditions.put("playEnty", playEnty);
        
        Map<String, Object> en = new HashMap<String, Object>();
        conditions.put("en", en);
        
        Map<String, Object> scopeEnty = new HashMap<String, Object>();
        conditions.put("scopeEnty", scopeEnty);

        Map<String, Object> playRole = new HashMap<String, Object>();
        playRole.put("roleId", wgId);
        conditions.put("playRole", playRole);

        Map<String, Object> playEnty2 = new HashMap<String, Object>();
        conditions.put("playEnty2", playEnty2);
        
        Map<String, Object> en2 = new HashMap<String, Object>();
        conditions.put("en2", en2);
        
        Map<String, Object> enu2 = new HashMap<String, Object>();
        enu2.put("use","L");
        conditions.put("enu2", enu2);
        
        //按病区中文名称、床号排序
        Map<String, Object> orderBy = new HashMap<String, Object>();
        orderBy.put("asc", "en2.formatted,en.formatted");
        conditions.put("order by", orderBy);
        
        List<PatientEncounter> encs = (List<PatientEncounter>) queryResults(conditions, PatientEncounter.class, pageNo, pageSize);
        return encs;
    }


    public Long findCountInPatientByWorkGroupIdByCond(String wgId, String criticalCode, String surgeryCode, String transfuseCode ) throws Hl7Exception
    {
        conditions.clear();

        Map<String, Object> enc = new HashMap<String, Object>();
        enc.put("codeEtsId", ActCode.INDEPARTMENT.code().stringValue());
        enc.put("statusCode", ActStatus.ACTIVE.code().stringValue());
        conditions.put("act", enc);

        Map<String, Object> cdSet = new HashMap<String, Object>();
        if (StrUtil.isNotAllEmpty(criticalCode, surgeryCode, transfuseCode))
        {
            if (StrUtil.isNotEmpty(criticalCode))
            {
                cdSet.put("attributeTypeCode", criticalCode);
            }
            else if (StrUtil.isNotEmpty(surgeryCode))
            {
                cdSet.put("attributeTypeCode", surgeryCode);
            }
            else if (StrUtil.isNotEmpty(transfuseCode))
            {
                cdSet.put("attributeTypeCode", transfuseCode);
            }
            conditions.put("acd", cdSet);
        }
        
        Map<String, Object> part = new HashMap<String, Object>();
        part.put("typeCode", ParticipationType.ELOC.code().toString());
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        r.put("codeEtsId", RoleCode.BED.code().toString());
        conditions.put("r", r);

        Map<String, Object> scopeEnty = new HashMap<String, Object>();
        conditions.put("scopeEnty", scopeEnty);

        Map<String, Object> playRole = new HashMap<String, Object>();
        playRole.put("roleId", wgId);
        conditions.put("playRole", playRole);
        
        Long count = (Long) queryCountResults(conditions, PatientEncounter.class);
        return count;
    }

    @SuppressWarnings("unchecked")
    public List<PatientEncounter> findOutPatientByWorkGroupIdByCond(String wgId, String leaveHospitalWayCode, String leaveHospitalConceptCode,
        String startTime,String endTime, int pageNo, int pageSize) throws Hl7Exception{
        conditions.clear();

        Map<String, Object> enc = new HashMap<String, Object>();
        enc.put("codeEtsId", ActCode.DISCHARGED.code().stringValue());
        //enc.put("statusCode", "active");
        
        if( StrUtil.isNotEmpty(startTime))
        {
            enc.put("activityStartDateTs>=", startTime);
        }
        if(StrUtil.isNotEmpty(endTime))
        {
            enc.put("activityStartDateTs<=", endTime);
        }
        conditions.put("act", enc);
        
        Map<String, Object> cdSet = new HashMap<String, Object>();
        cdSet.put("attributeTypeCode", leaveHospitalWayCode);
        if( StrUtil.isNotEmpty(leaveHospitalConceptCode)){
            cdSet.put("conceptCode", leaveHospitalConceptCode);
        }
        conditions.put("acd", cdSet);
        
        Map<String, Object> part = new HashMap<String, Object>();
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        r.put("roleId", wgId);
        r.put("codeEtsId", RoleCode.WORKGROUP.code().toString());
        conditions.put("r", r);
        
        Map<String, Object> playEnty = new HashMap<String, Object>();
        conditions.put("playEnty", playEnty);
        
        Map<String, Object> en = new HashMap<String, Object>();
        conditions.put("en", en);
        
        Map<String, Object> enu = new HashMap<String, Object>();
        enu.put("use","L");
        conditions.put("enu", enu);
        
        //按病区中文名称排序
        Map<String, Object> orderBy = new HashMap<String, Object>();
        orderBy.put("asc", "en.formatted");
        conditions.put("order by", orderBy);
        
        Query query = em.createQuery(constructJbql(conditions, PatientEncounter.class, null, null));
        if (StrUtil.isNotEmpty(startTime))
        {
            Date from = DateUtil.getDateBy3String(startTime);
            query.setParameter("from", from, TemporalType.TIMESTAMP);
        }
        if (StrUtil.isNotEmpty(endTime))
        {
            Date to = DateUtil.getDateBy3String(endTime);
            query.setParameter("to", to, TemporalType.TIMESTAMP);
        }
        if( pageNo > 0 && pageSize > 0 ){
            query.setFirstResult((pageNo - 1) * pageSize).setMaxResults(pageSize);
        }
        List<PatientEncounter> encs = query.getResultList();
        return encs;
        /*List<PatientEncounter> encs = (List<PatientEncounter>) queryResults(conditions, PatientEncounter.class, pageNo, pageSize);
        return encs;*/
        
    }
    
    public Long findCountOutPatientByWorkGroupIdByCond(String wgId, String leaveHospitalWayCode, String leaveHospitalConceptCode,String startTime,
        String endTime) throws Hl7Exception{
        
        conditions.clear();

        Map<String, Object> enc = new HashMap<String, Object>();
        enc.put("codeEtsId", ActCode.DISCHARGED.code().stringValue());
        //enc.put("statusCode", "active");
        
        if( StrUtil.isNotEmpty(startTime))
        {
            enc.put("activityStartDateTs>=", startTime);
        }
        if(StrUtil.isNotEmpty(endTime))
        {
            enc.put("activityStartDateTs<=", endTime);
        }
        conditions.put("act", enc);
        
        Map<String, Object> cdSet = new HashMap<String, Object>();
        cdSet.put("attributeTypeCode", leaveHospitalWayCode);
        if( StrUtil.isNotEmpty(leaveHospitalConceptCode)){
            cdSet.put("conceptCode", leaveHospitalConceptCode);
        }
        conditions.put("acd", cdSet);
        
        Map<String, Object> part = new HashMap<String, Object>();
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        r.put("roleId", wgId);
        conditions.put("r", r);
        
        Query query = em.createQuery(constructJbql(conditions, PatientEncounter.class, Long.class, null));
        if (StrUtil.isNotEmpty(startTime))
        {
            Date from = DateUtil.getDateBy3String(startTime);
            query.setParameter("from", from, TemporalType.TIMESTAMP);
        }
        if (StrUtil.isNotEmpty(endTime))
        {
            Date to = DateUtil.getDateBy3String(endTime);
            query.setParameter("to", to, TemporalType.TIMESTAMP);
        }
        Long count = (Long) query.getResultList().get(0);
        return count;
        /*List<PatientEncounter> encs = (List<PatientEncounter>) queryResults(conditions, PatientEncounter.class, pageNo, pageSize);
        return encs;*/
    }
        

    @SuppressWarnings("unchecked")
    public List<PatientEncounter> findInPatientByWorkGroupIdAndTrackFlag(String workGroupId, String trackFlag, int pageNo, int pageSize)
        throws Hl7Exception
    {
        conditions.clear();

        Map<String, Object> enc = new HashMap<String, Object>();
        enc.put("codeEtsId", ActCode.INDEPARTMENT.code().stringValue());
        enc.put("negationFlag", trackFlag);
        enc.put("statusCode", ActStatus.ACTIVE.code().stringValue());
        conditions.put("act", enc);

        Map<String, Object> part = new HashMap<String, Object>();
        part.put("typeCode", ParticipationType.ELOC.code().toString());
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        r.put("codeEtsId", RoleCode.BED.code().toString());
        conditions.put("r", r);

        Map<String, Object> playEnty = new HashMap<String, Object>();
        conditions.put("playEnty", playEnty);
        
        Map<String, Object> en = new HashMap<String, Object>();
        conditions.put("en", en);
        
        Map<String, Object> scopeEnty = new HashMap<String, Object>();
        conditions.put("scopeEnty", scopeEnty);

        Map<String, Object> playRole = new HashMap<String, Object>();
        playRole.put("roleId", workGroupId);
        conditions.put("playRole", playRole);

        Map<String, Object> playEnty2 = new HashMap<String, Object>();
        conditions.put("playEnty2", playEnty2);
        
        Map<String, Object> en2 = new HashMap<String, Object>();
        conditions.put("en2", en2);
        
        Map<String, Object> enu2 = new HashMap<String, Object>();
        enu2.put("use","L");
        conditions.put("enu2", enu2);
        
      //按病区中文名称、床号排序
        Map<String, Object> orderBy = new HashMap<String, Object>();
        orderBy.put("asc", "en2.formatted,en.formatted");
        conditions.put("order by", orderBy);
        
        List<PatientEncounter> encs = (List<PatientEncounter>) queryResults(conditions, PatientEncounter.class, pageNo, pageSize);
        return encs;
    }


    public Long findCountInPatientByWorkGroupIdAndTrackFlag(String workGroupId, String trackFlag) throws Hl7Exception
    {
        conditions.clear();

        Map<String, Object> enc = new HashMap<String, Object>();
        enc.put("codeEtsId", ActCode.INDEPARTMENT.code().stringValue());
        enc.put("negationFlag", trackFlag);
        enc.put("statusCode", ActStatus.ACTIVE.code().stringValue());
        conditions.put("act", enc);

        Map<String, Object> part = new HashMap<String, Object>();
        part.put("typeCode", ParticipationType.ELOC.code().toString());
        conditions.put("part", part);

        Map<String, Object> r = new HashMap<String, Object>();
        r.put("codeEtsId", RoleCode.BED.code().toString());
        conditions.put("r", r);

        Map<String, Object> scopeEnty = new HashMap<String, Object>();
        conditions.put("scopeEnty", scopeEnty);

        Map<String, Object> playRole = new HashMap<String, Object>();
        playRole.put("roleId", workGroupId);
        conditions.put("playRole", playRole);

        Long count = queryCountResults(conditions, PatientEncounter.class);
        return count;
    }


    public PatientEncounter findPatientEncounterByActId(String actId) throws Hl7Exception
    {
        PatientEncounter patientEncounter = (PatientEncounter) findActByActId(Long.valueOf(actId));
        return patientEncounter;
    }
}
